from django.contrib.auth.models import User
from django.shortcuts import render
from django.contrib import auth


# Create your views here.
def login_index(request):
    login_result = ''
    try:
        username = request.POST['username']
        password = request.POST['password']
        user = User.objects.get(username=username)
        if user.check_password(password):
            auth.login(request, user)
            # Login进行登录，将登陆信息进行记录在了session中
            # auth.logout(request) # 退出登录，删除session中登陆信息
            # text = '登陆成功，欢迎' + user.username
            # return HttpResponse(text)
            return render(request, 'pmcs/index.html')
    except Exception as e:
        print(e)
    else:
        login_result = '密码错误，请检查后重新输入！'
    context = {
        'login_result': login_result
    }

    return render(request, 'pmcs/login.html', context=context)
